package com.example.ketang1213;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        saveDataWithFile();
    }
    private void saveDataWithFile(){
        String filename="myFile.txt";
       File file= new File(getExternalCacheDir(),filename);
       try{
        FileOutputStream fileOutputStream=new FileOutputStream(file);
        fileOutputStream.write("hello word".getBytes());
        fileOutputStream.close();

        FileInputStream fileInputStream=new FileInputStream(file);
        byte[] bytes=new byte[20];
        fileInputStream.read(bytes);
           Log.d("MainActivity","readDataWithFileByFileInputStream"+new String(bytes,"UTF-8"));
           fileInputStream.close();

        fileInputStream=new FileInputStream(file);
           BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
           String s=bufferedReader.readLine();
           Log.d("MainActivity","readDataWithFile BufferedReader "+s);
           fileInputStream.close();
          bufferedReader.close();
       }
       catch (Exception e){
           e.printStackTrace();
       }
    }
}
